package com.zmn.oms.dubbo.impl.zmn.normal.serviceitem;

import org.apache.dubbo.config.annotation.Service;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.dto.zmn.serviceitem.AddOrderServiceItemDIO;
import com.zmn.oms.dubbo.dto.zmn.serviceitem.OrderProductExtendDRO;
import com.zmn.oms.dubbo.dto.zmn.serviceitem.OrderServiceItemDRO;
import com.zmn.oms.dubbo.interfaces.zmn.normal.serviceitem.ZsNormalOrderServiceItemRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.serviceitem.AddOrderServiceItemDTO;
import com.zmn.oms.model.entity.product.OrderProductExtend;
import com.zmn.oms.model.entity.serviceitem.OrderServiceItem;
import com.zmn.oms.zmn.normal.business.interfaces.serviceitem.ZsNormalOrderServiceItemBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 类描述：啄木鸟-普通工单-服务项远程调用
 *
 * @author heciqi
 * @date 2018/12/06 15:11
 */
@Service(loadbalance = "roundrobin", timeout = 5000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class ZsNormalOrderServiceItemRemoteServiceImpl implements ZsNormalOrderServiceItemRemoteService {

	@Autowired
	private ZsNormalOrderServiceItemBService zsNormalOrderServiceItemBService;

	/**
	 * 获取订单产品扩展
	 *
	 * @param orderId
	 * @return
	 */
	@Override
	public ResponseDTO<OrderProductExtendDRO> getOrderProductExtend(Long orderId) throws OmsBaseException {
		List<OrderProductExtend> orderProductExtends = zsNormalOrderServiceItemBService.getOrderProductExtend(orderId);
		// 转换
		List<OrderProductExtendDRO> orderProductExtendDROS = BeanMapper.mapList(orderProductExtends, OrderProductExtendDRO.class);
		return ResponseUtil.responseSuccess(orderProductExtendDROS, null);
	}

	/**
	 * 根据订单id查询服务项列表
	 *
	 * @param orderId
	 * @return
	 */
	@Override
	public ResponseDTO<OrderServiceItemDRO> listByOrderId(Long orderId) {

		List<OrderServiceItem> orderServiceItems = zsNormalOrderServiceItemBService.listByOrderId(orderId);
		// 转换
		List<OrderServiceItemDRO> orderServiceItemDROS = BeanMapper.mapList(orderServiceItems, OrderServiceItemDRO.class);

		return ResponseUtil.responseSuccess(orderServiceItemDROS, null);
	}

	/**
	 * 新增订单服务项
	 *
	 * @param addOrderServiceItemDIO
	 */
	@Override
	public ResponseDTO insertServiceItem(AddOrderServiceItemDIO addOrderServiceItemDIO){

		AddOrderServiceItemDTO addOrderServiceItemDTO = new AddOrderServiceItemDTO();
		addOrderServiceItemDTO.setOrderId(addOrderServiceItemDTO.getOrderId());
		addOrderServiceItemDTO.setProExtId(addOrderServiceItemDTO.getProExtId());
		addOrderServiceItemDTO.setQuotationType(GlobalConsts.OPERATE_USER_TYPE_MASTER);
		if (!CollectionUtils.isEmpty(addOrderServiceItemDIO.getServiceItemDIOS())) {
			addOrderServiceItemDTO.setOrderServiceItemList(BeanMapper.mapList(addOrderServiceItemDIO.getServiceItemDIOS(),OrderServiceItem.class));
		}
		try {
			zsNormalOrderServiceItemBService.insertServiceItem(addOrderServiceItemDTO);
			return ResponseUtil.responseSuccess(null,null);
		} catch (Exception e) {
			return ResponseUtil.responseFail(e.getMessage());
		}
	}
}
